package net.lmc.realtime.jtp.jtprealtimeservice.controller;

import com.alibaba.fastjson.JSON;
import net.lmc.realtime.jtp.jtprealtimeservice.bean.TrafficPvReport;
import net.lmc.realtime.jtp.jtprealtimeservice.bean.TrafficUvReport;
import net.lmc.realtime.jtp.jtprealtimeservice.service.TrafficChannelStatsService;
import net.lmc.realtime.jtp.jtprealtimeservice.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/jtp/traffic")
public class TrafficController {
    @Autowired
    private TrafficChannelStatsService  trafficChannelStatsService;

    private static final String DATE_DEFAULT_VALUE = "1970-07-01";

    @RequestMapping("/UvCount")
    public String getUvCount(@RequestParam(value = "date",defaultValue = DATE_DEFAULT_VALUE)String date){
        if (DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtil.nowDate();
        }

        List<TrafficUvReport> trafficUvReportList = trafficChannelStatsService.getUvCount(date);

        if(null == trafficUvReportList || trafficUvReportList.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(trafficUvReportList);
    }


    @RequestMapping("/PvCount")
    public String getPvCount(@RequestParam(value = "date",defaultValue = DATE_DEFAULT_VALUE)String date){
        if (DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtil.nowDate();
        }

        List<TrafficPvReport> trafficPvReportList = trafficChannelStatsService.getPvCount(date);

        if(null == trafficPvReportList || trafficPvReportList.isEmpty()){
            return "[]";
        }
        return JSON.toJSONString(trafficPvReportList);
    }
}
